# -*- bazel -*-

load("@drake//tools/install:install.bzl", "install")
load("@drake//tools/skylark:cc.bzl", "cc_library")
load(
    "@drake//tools/workspace:cmake_configure_file.bzl",
    "cmake_configure_file",
)

licenses(["notice"])  # Apache-2.0

package(
    default_visibility = ["//visibility:public"],
)

# Generates osqp_configure.h based on the defines= we want in Drake.
cmake_configure_file(
    name = "configure_file",
    src = "configure/osqp_configure.h.in",
    out = "include/public/osqp_configure.h",
    defines = [
        "OSQP_ENABLE_PRINTING",
        "OSQP_ENABLE_PROFILING",
        # Keep the default primitive size of `double` and `int`.  Don't define
        # 'DFLOAT' nor 'DLONG' unless @qdldl_internal and @scs_internal are
        # also changed to use those primitive sizes.
        # See drake/tools/workspace/qdldl_internal/README.md.
    ] + select({
        "@drake//tools/cc_toolchain:apple": [
            "IS_MAC",
        ],
        "@drake//tools/cc_toolchain:linux": [
            "IS_LINUX",
        ],
        "//conditions:default": [],
    }),
    undefines = [
        "OSQP_CUSTOM_MEMORY",
    ],
    visibility = ["//visibility:private"],
)

# This list matches osqp_headers_private in CMakeLists.txt.
OSQP_HEADERS_PRIVATE = [
    "include/private/version.h",
    "include/private/algebra_vector.h",
    "include/private/algebra_matrix.h",
    "include/private/auxil.h",
    "include/private/error.h",
    "include/private/glob_opts.h",
    "include/private/lin_alg.h",
    "include/private/printing.h",
    "include/private/profilers.h",
    "include/private/scaling.h",
    "include/private/timing.h",
    "include/private/types.h",
    "include/private/util.h",
]

# This list matches osqp_headers_public in CMakeLists.txt.
OSQP_HEADERS_PUBLIC = [
    "include/public/osqp.h",
    "include/public/osqp_api_constants.h",
    "include/public/osqp_api_functions.h",
    "include/public/osqp_api_types.h",
    "include/public/osqp_export_define.h",
] + [
    # This header is part of osqp_headers_public but is added in CMakeLists.txt
    # in a separate stanza, so we do the same here.
    "include/public/osqp_configure.h",
] + [
    # This header is enabled in CMakeLists only when building in non-embedded
    # mode.
    "include/private/polish.h",
]

OSQPLIB_SOURCES = [
    # From ./src/CMakeLists.txt.
    # These sources are always enabled in CMakeLists.
    "src/auxil.c",
    "src/error.c",
    "src/scaling.c",
    "src/util.c",
    # These sources are enabled in CMakeLists only when building in
    # non-embedded mode.
    "src/polish.c",
    # This seesm to be missing from CMakeLists.txt; oh well.
    "src/osqp_api.c",
    # From algebra/builtin/CMakeLists.txt:
    "algebra/_common/csc_math.h",
    "algebra/_common/csc_math.c",
    "algebra/_common/csc_utils.h",
    "algebra/_common/csc_utils.c",
    "algebra/builtin/algebra_impl.h",
    "algebra/builtin/algebra_libs.c",
    "algebra/builtin/vector.c",
    "algebra/builtin/matrix.c",
    # From algebra/_common/lin_sys/qdldl/qdldl.cmake:
    "algebra/_common/kkt.h",
    "algebra/_common/kkt.c",
    "algebra/_common/lin_sys/qdldl/qdldl_interface.h",
    "algebra/_common/lin_sys/qdldl/qdldl_interface.c",
] + select({
    # These sources are enabled in CMakeLists only when building with
    # OSQP_ENABLE_PROFILING enabled.
    "@drake//tools/cc_toolchain:apple": [
        "src/timing_macos.c",
    ],
    "//conditions:default": [
        "src/timing_linux.c",
    ],
})

cc_library(
    name = "osqp",
    hdrs = OSQP_HEADERS_PUBLIC,
    srcs = OSQP_HEADERS_PRIVATE + OSQPLIB_SOURCES,
    includes = [
        "include/private",
        "include/public",
        "algebra/builtin",
        "algebra/_common",
        "algebra/_common/lin_sys/qdldl",
    ],
    isystem = True,
    copts = [
        "-fvisibility=hidden",
        "-w",
        "-Werror=incompatible-pointer-types",
    ],
    linkstatic = 1,
    deps = [
        "@qdldl_internal//:qdldl",
        "@suitesparse_internal//:amd",
    ],
)

install(
    name = "install",
    docs = ["LICENSE"],
    doc_strip_prefix = ["lin_sys/direct/qdldl"],
)

exports_files(["drake_repository_metadata.json"])
